package cn.lili.controller.user.union;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.Union;
import cn.lili.modules.member.entity.dto.UnionAddDTO;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import cn.lili.modules.order.order.entity.vo.OrderVO;
import cn.lili.modules.order.order.entity.vo.PerformanceDetailVO;
import cn.lili.modules.order.order.entity.vo.UnionMonthFundsVO;
import cn.lili.modules.order.order.entity.vo.UnionOrderParams;
import cn.lili.modules.union.service.UnionLogService;
import cn.lili.modules.union.service.UnionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "工会管理接口")
@RestController
@RequestMapping("/user/union")
@RequiredArgsConstructor
@Validated
public class UnionController {

    private final UnionService unionService;

    @ApiOperation(value = "添加工会")
    @PostMapping(value = "/add")
    public ResultMessage<Object> add(@Valid @RequestBody UnionAddDTO unionAddDTO) {
        unionService.add(unionAddDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取工会分页列表")
    @GetMapping
    public ResultMessage<Page<Union>> getByPage(Union union, PageVO page) {
        return ResultUtil.data(unionService.getByPage(union, page));
    }

    @ApiOperation(value = "根据ID获取工会信息")
    @ApiImplicitParam(name = "unionId", value = "工会ID", required = true, paramType = "path", dataType = "String")
    @PutMapping(value = "/{id}")
    public ResultMessage<Union> getUnionById(@Valid @PathVariable String id) {
        return ResultUtil.data(unionService.getUnionById(id));
    }

    @ApiOperation(value = "变更会长")
    @PostMapping(value = "/updateChairman")
    public ResultMessage<Object> updateChairman(@Valid @RequestBody Union union) {
        unionService.updateChairman(union);
        return ResultUtil.success();
    }


    /**
     * 前端调用
     * 搜索工会，周边工会
     * @param union
     * @param page
     * @return
     */
    @ApiOperation(value = "搜索工会，周边工会,获取工会分页列表，仅支持按城市和名称查询")
    @GetMapping("/searchByPage")
    public ResultMessage<Page<Union>> searchByPage(Union union, PageVO page) {
        return ResultUtil.data(unionService.searchByPage(union, page));
    }



    /**
     * 前端调用
     * 今日首单
     * @return
     */
    @ApiOperation(value = "工会今日首单,接口返回的code值为1，表示无数据，为200，则有数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    @GetMapping("/todayFirstOrder")
    public ResultMessage<OrderVO> todayFirstOrder(String unionId) {
        OrderVO data = unionService.todayFirstOrder(unionId);
        if(null == data){
            return ResultUtil.error(1,"今日无数据");
        }
        return ResultUtil.data(data);
    }

    @ApiOperation(value = "解散工会")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工会ID", required = true,paramType = "query"),
    })
    @GetMapping("/deleteUnion")
    public ResultMessage<Object> deleteUnion (String id) {
        unionService.deleteUnion(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "今日订单列表，支持分页，降序排列")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    @GetMapping("/todayOrderByPage")
    public ResultMessage<Page<OrderVO>> todayOrderByPage(String unionId,Integer pageNo,Integer pageSize) {
        if(null == pageNo){
            pageNo = 1;
        }
        if(null == pageSize){
            pageSize = 10;
        }
        return ResultUtil.data(unionService.todayOrderByPage(unionId,pageNo,pageSize));
    }

    @ApiOperation(value = "工会经费明细")
    @GetMapping("/unionMoneyDetail")
    public ResultMessage<Page<OrderFlow>> unionMoneyDetail(UnionOrderParams params) {
        return ResultUtil.data(unionService.getUnionMoneyDetail(params));
    }

    @ApiOperation(value = "工会经费明细统计")
    @GetMapping("/getUnionMoneyStatistics")
    public ResultMessage<PerformanceDetailVO> getUnionMoneyStatistics(UnionOrderParams params) {
        return ResultUtil.data(unionService.getUnionMoneyStatisticsDetail(params));
    }

    @ApiOperation(value = "修改工会信息")
    @PostMapping("/updateUnion")
    public ResultMessage<Object> updateUnion (@RequestBody Union union) {
        unionService.updateUnion(union);
        return ResultUtil.success();
    }

    @ApiOperation(value = "分页获取工会经费发放记录")
    @GetMapping("/getUnionFundsByPage")
    public ResultMessage<Page<UnionMonthFundsVO>> getUnionFundsByPage (String unionId) {
        return ResultUtil.data(unionService.getUnionFundsByPage(unionId));
    }

    @ApiOperation(value = "获取工会本月分销订单数量、已完成订单数量、未完成订单数量、已退单数量")
    @GetMapping("/getCurrentOrderPrice")
    public ResultMessage<PerformanceDetailVO> getCurrentOrderPrice (String unionId) {
        return ResultUtil.data(unionService.getCurrentOrderCount(unionId));
    }

    @ApiOperation(value = "获取工会本月分销订单数量、已完成订单数量、未完成订单数量、已退单数量")
    @GetMapping("/getCurrentOrderCount")
    public ResultMessage<PerformanceDetailVO> getCurrentOrderCount (String unionId) {
        return ResultUtil.data(unionService.getCurrentOrderCount(unionId));
    }

    @ApiOperation(value = "获取工会上月分销订单数量、已完成订单数量、未完成订单数量、已退单数量")
    @GetMapping("/getLastOrderCount")
    public ResultMessage<PerformanceDetailVO> getLastOrderCount (String unionId) {
        return ResultUtil.data(unionService.getLastOrderCount(unionId));
    }

    @ApiOperation(value = "获取工会城市列表")
    @GetMapping("/getCityListByProvinceId")
    public ResultMessage<List<Union>> getCityListByProvinceId () {
        return ResultUtil.data(unionService.getCityListByProvinceId());
    }
}
